package com.yeheng.controller;


import com.yeheng.dto.Result;
import com.yeheng.entity.Dept;
import com.yeheng.entity.Emp;
import com.yeheng.service.DeptSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

/**
 * @PackgeName: com.yeheng.homework.controller
 * @ClassName: DeptController
 * @Author: YH
 * Date: 2020/12/26 21:54
 * project name: homework
 * @Version:
 * @Description:
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptSerivce serivce;

    @RequestMapping(value = "/findAll",method = RequestMethod.POST)
    @ResponseBody
    public Result findAll(){
        Result result = new Result();
        result.setResult(serivce.findAll());
        result.setCode(0);
        result.setMsg("");
        result.setCount(0);
        return result;
    }

    @RequestMapping(value = "/updateDept",method = RequestMethod.POST)
    @ResponseBody
    public Result updateDept(@RequestBody Dept dept){
        Result result = new Result();
        result.setResult(null);
        result.setCode(serivce.updateDept(dept)==true?0:-1);
        result.setMsg("");
        result.setCount(0);
        return result;
    }

    @RequestMapping(value = "/delByDid",method = RequestMethod.POST)
    @ResponseBody
    public Result delDid(Integer did){
        Result result = new Result();
        result.setResult(null);
        result.setCode(serivce.delDid(did)?0:-1);
        result.setMsg("");
        result.setCount(0);
        return result;
    }


    @RequestMapping(value = "/addDept",method = RequestMethod.POST)
    @ResponseBody
    public Result addDept(@RequestBody @Validated Dept dept, Errors errors){
        Result result = new Result();
        if (errors.hasErrors()){
            result.setCode(-1);
            result.setMsg(errors.getFieldErrors().get(0).getDefaultMessage());
        }else {
            result.setResult(null);
            result.setCode(serivce.addDept(dept) ? 0 : -1);
            result.setMsg("");
            result.setCount(0);
        }
        return result;
    }

    @RequestMapping(value = "/delMany",method = RequestMethod.POST)
    @ResponseBody
    public Result delMany(@RequestBody List<Integer> list){
        Result result = new Result();
        result.setResult(null);
        result.setCode(serivce.delMany(list)?0:-1);
        result.setMsg("");
        result.setCount(0);
        return result;
    }

    @PostMapping(value = "/{id}")
    public String test(@PathVariable("id") Integer id){
        System.out.println(id);
        return "dept";
    }



}
